package weather.util.dictionary;

import java.io.*;
import java.util.*;

import org.apache.poi.ss.usermodel.*;

/**
 * @author Jerzy Mazurek WEiTI PW
 */
public abstract class AbstractMappingDictionary<Key, Value> extends AbstractDictionary
{
	public void loadFromXlsFile(File path, int sheetNumber) throws IOException
	{
		Workbook wb = openWorkbook(path);
		Sheet sheet = wb.getSheetAt(sheetNumber);

		int keyCellNum = sheet.getRow(sheet.getFirstRowNum()).getFirstCellNum();
		int valueCellNum = keyCellNum + 1;
		
		map.clear();
		for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); ++r)
		{
			Row row = sheet.getRow(r);

			map.put(newKey(row.getCell(keyCellNum).getStringCellValue()),
					newValue(row.getCell(valueCellNum).getStringCellValue()));
		}
	}

	public Set<Key> getKeys()
	{
		return map.keySet();
	}
	
	public boolean containsKey(Key key)
	{
		return map.containsKey(key);
	}

	public Value getValue(Key key)
	{
		return map.get(key);
	}

	protected abstract Key newKey(String s);
	protected abstract Value newValue(String s);
	
	protected Map<Key, Value> getMap()
	{
		return map;
	}
	
	private Map<Key, Value> map = new HashMap<Key, Value>();
}
